#pragma once

#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>

class Util{
public:
  //把结果都读到out里
 static int ReadLine(int sock,std::string&out)
 {
     char ch='X';
     while(ch !='\n'){
         ssize_t s=recv(sock,&ch,1,0);
         if(s>0)//读取成功
         {
             if(ch=='\r')
             {
                recv(sock,&ch,1,MSG_PEEK);//窥探下一位是不是\n
                if(ch=='\n')
                { //即这里为\r\n
                  //窥探成功
                  recv(sock,&ch,1,0);//再读取ch
                }
                else{
                    ch='\n';
                }
                
             }
             out.push_back(ch);
         }
        else if(s==0){
            return 0;
        } 
        else{
            return -1;
        }
     }
     return out.size();
 }
 //截取字符串，将报头信息进行截取
 static bool CutString(const std::string target,std::string &sub1_out,std::string &sub2_out,std::string sep)
 {
    size_t pos=target.find(sep);//找分隔符位置
    if(pos!=std::string::npos){
        sub1_out=target.substr(0,pos);
        sub2_out=target.substr(pos+sep.size());//不标长度自动截取到结尾
        return true;
    }
    return false;
 }

};